function excel(curTable)
{
   if(38==event.keyCode||40==event.keyCode){changeTr(curTable,event.keyCode);}
   if(37==event.keyCode||39==event.keyCode){changeTd(curTable,event.keyCode);}
}
function changeTr(curTable,curKeyCode)
{

   var trs=curTable.rows;
   var trNum=1;
   var tdNum=0;
   for(var i=1;i<6;i++)
   {
      if(trs[i].style.backgroundColor=="#b0d4ea")
      {
         
         productMoney();
      
         if(38==curKeyCode)trNum=i-1;
         if(40==curKeyCode)trNum=i+1;
         if(trNum<1)trNum=1;
         if(trNum>(5))trNum=5;
         
         var inputs=trs[i].getElementsByTagName("INPUT");
         for(var j=0;j<inputs.length;j++)
         {
             if(inputs[j]==document.activeElement)
             { tdNum=j; } 
         }
         if(tdNum<0)tdNum=0;
         if(tdNum>(inputs.length))tdNum=inputs.length;
         
         sumMoney();
         
         break;
      }
   }
   for(var i=1;i<6;i++)
   {
      trs[i].style.backgroundColor="#ffffff";
   }
   trs[trNum].style.backgroundColor="#b0d4ea";
   trs[trNum].getElementsByTagName("INPUT")[tdNum].focus();
   trs[trNum].getElementsByTagName("INPUT")[tdNum].createTextRange().select();
   
   
 }            
function changeTd(curTable,curKeyCode)
{
   var trs=curTable.rows;
   for(var i=1;i<6;i++)
   {
      if(trs[i].style.backgroundColor=="#b0d4ea")
      {
         var inputs=trs[i].getElementsByTagName("INPUT");
         for(var j=0;j<inputs.length;j++)
         {
            if(inputs[j]==document.activeElement)
            {
               var iNum=0;
               if(isBegin(inputs[j])&&37==curKeyCode&&j!=0)
               {  iNum=j-1;
                  if(iNum<0)iNum=0;
                  if(iNum>(inputs.length-1))iNum=inputs.length-1;
                  inputs[iNum].focus();
                  inputs[iNum].createTextRange().select();
                  return;
               }
               if(isEnd(inputs[j])&&39==curKeyCode&&j!=inputs.length-1)
               {  iNum=j+1;
                  if(iNum<0)iNum=0;
                  if(iNum>(inputs.length-1))iNum=inputs.length-1;
                  inputs[iNum].focus();
                  inputs[iNum].createTextRange().select();
               }
               return;
            } 
         }
         return;
      }  
   }
}
function isBegin(curInput)
{
   var focusRange=document.selection.createRange();
   focusRange.setEndPoint("StartToStart",curInput.createTextRange());
   if(0==focusRange.text.length)
     return true;
   else
     return false;
}
function isEnd(curInput)
{
   var focusRange=document.selection.createRange();
   focusRange.setEndPoint("StartToStart",curInput.createTextRange());
   if(curInput.value.length==focusRange.text.length)
     return true;
   else
     return false;
}
function productMoney()
{
    var trs=document.getElementById("bargainProduct").rows;
    for(var i=1;i<6;i++)
    {
         var cells=trs[i].cells;
         var pars=new Array();
         var rNum=0;
         for(var j=4;j<cells.length;j++)
         {
            var cell=cells[j];
            var inputs=cell.getElementsByTagName("INPUT");
            if(1==inputs.length&&inputs[0].value!="")
            {
               pars.push(inputs[0].value);
               rNum=j+1;
            }
         }
         if(pars.length==2)
         {
            cells[rNum].innerHTML=(pars[0]*pars[1]).toFixed(2);
         }
    }
}
function sumMoney()
{ 
   var trs=document.getElementById("bargainProduct").rows;
   var sum=0;
   for(var j=1;j<6;j++)
   {
       if(trs[j].cells[6].innerHTML!="")
       {
          sum+=parseFloat(trs[j].cells[6].innerHTML);
       }
   }
  
   document.getElementById("countC").innerHTML=convertCurrency(sum);
   document.getElementById("count").innerHTML=standandNum(sum.toFixed(2));
}